package com.example.alipay.controller;



import cn.org.bjca.client.exceptions.*;
import cn.org.bjca.client.security.SecurityEngineDeal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: xuke
 * @time: 2021/4/2 0:35
 */
@Controller
public class DsvsController {
    Boolean islogin =false;
    SecurityEngineDeal sed = null;
    SecurityEngineDeal sedTss = null;

    //服务端所需
    String strServerCert = null;  //声明一个DSVS服务器证书变量
    String strRandom = null; //声明一个随机数变量
    String strSignedData = null;

    @GetMapping("/to_dsvs_login")
    public String index(HttpSession session) throws Exception {
        try {
            sed = SecurityEngineDeal.getInstance("SVSDefaultSM2");
            if (sed != null) {
                System.out.println("DSVS链接成功");
            }
            else {
                return "networkerror";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "networkerror";
        }


        //**************************************服务端产生三个参数，随机数，服务器证书，对随机数的签名值******************************************
         strServerCert = sed.getServerCertificate();
         strRandom = sed.genRandom(24);
         strSignedData = sed.signData(strRandom.getBytes());

        //将服务器端产生的随机数放入session中
        session.setAttribute("strRandom", strRandom);
        session.setAttribute("strServerCert", strServerCert);
        session.setAttribute("strSignedData", strSignedData);
        System.out.println("服务端随机数:" + strRandom);
        System.out.println("服务端证书:" + strServerCert);
        System.out.println("服务端对随机数签名值:" + strSignedData);


        return "dsvs/dsvslogin";

    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session) throws ApplicationNotFoundException, InitException, SVSConnectException, ParameterOutRangeException, ParameterTooLongException, ParameterInvalidException, UnkownException {
        System.out.println("收到前端登录请求");




        String requestURI = request.getRequestURI();
        System.out.println("Request URI: " + requestURI);

        //**************************************服务端验证******************************************
        String strContainerName = (String) request.getParameter("ContainerName");
        String UserCert = (String) request.getParameter("UserCert");
        String UserSignedData = (String) request.getParameter("UserSignedData");
        String strRandom2 = (String) request.getParameter("strRandom");
        System.out.println("strContainerName:" + strContainerName);
        System.out.println("UserCert:" + UserCert);
        System.out.println("UserSignedData:" + UserSignedData);
        System.out.println("strRandom:" + strRandom);

        //******************************服务端开始验签**********************************
        if (UserCert == null || strRandom2 == null || UserSignedData == null) {
            System.out.println("传参失败");
            return "dsvs/error";
        }
        try {
             boolean b = sed.verifySignedData(UserCert, strRandom2, UserSignedData);
            //boolean b = sed.verifySignedData(UserCert, "N4AoJhDFvxstgFrHK3+T3/cDEnVubN8V", "SRG0CRAmjZdTSBuBWynsizjcqX3OATy/tDG6IC9ucWRkyNWXMB+yEFJ6T0CqDxfGjh0l56Z+L3LEYvqieamzDmxa+GDikRmfVi/EyiOiSUx/7JYZWpj/XlKQjwdqxZ2ISYMDbfSrmblp+r4nO5lHTLMoJnzBYekfq398WnYzKKM=");
            System.out.println(b == true  && islogin==true? "验证签名成功" : "验证签名失败");
            if (b == true && islogin==true) {
                return "dsvs/successful";
            } else {
//                return "index3";
                return "dsvs/error";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "dsvs/error";
        }
    }

    @RequestMapping("/doregister")
    public String doregister(HttpServletRequest request, HttpSession session) throws ApplicationNotFoundException, InitException, SVSConnectException, ParameterOutRangeException, ParameterTooLongException, ParameterInvalidException, UnkownException {
        islogin=true;
        return "dsvs/registerSuccess";
    }

}